<script setup lang="ts">
import { computed } from 'vue';
import dayjs from 'dayjs';
import runData from '@/components/ECharts/runData/index.vue';
import { MapData } from '@/types/MapData';
import { getModelData, getPointInfo } from '@/api/earlyWarning';
import alarmRecords from '@/components/alarmRecords/index.vue';
import EarlyWarningTickets from '@/components/EarlyWarningTickets/index.vue';
import { useIntelligentAnalyticsStore } from '@/store/modules/IntelligentAnalytics/useIntelligentAnalyticsStore';
import router from '@/router';
import empty from '@/assets/icons/svg/empty-box.svg';
import { useAtlasStore } from '@/store/modules/atlas';
import { adjustWebSocketUrl } from '@/utils/adjustWebSocketUrl';
const emit = defineEmits(['update:mapData']);
const props = defineProps<{
  mapData: MapData;
  id: string | number;
  someDay: string;
}>();
const isShowBtn = ref(false);
const alarmRecordsRef = ref();
const alarmTimePeriod = ref('');
//根据报警级别获取报警事件
const getAlarmTime = computed(() => {
  const data = props.mapData.info;
  if (data.alarmType == '05') {
    return '实时视频';
  } else {
    if (data.status == '3') {
      //这里面代表着已处置 时间就是开始时间到处置时间
      return `设备运行数据监测`;
    } else {
      //否则就是还没有处置完成的就是从开始时间到现在
      return `设备运行数据监测`;
    }
  }
});
// 监听报警级别变化 并通过报警时间 来渲染时间段
watch(
  () => props.mapData.info,
  (newVal) => {
    if (newVal.alarmType == '05') {
      alarmTimePeriod.value = '';
    } else {
      //这个判断是因为获取设备的运行数据之后 导致报警的时间消失了  所以判断 如果要是没有报警事件就直接return
      if (!newVal.reportTime && !newVal.processTime) return;
      if (newVal.status == '3') {
        alarmTimePeriod.value = `(${newVal.reportTime.slice(0, 10) + '至' + newVal.processTime.slice(0, 10)})`;
      } else {
        alarmTimePeriod.value = `(${newVal.reportTime.slice(0, 10) + '至' + dayjs(new Date().getTime()).format('YYYY-MM-DD')})`;
      }
    }
  },
  { immediate: true }
);
const ReportHandle = () => {
  alarmRecordsRef.value.open();
};
// 判断是否显示echarts
const isShowECharts = computed(() => {
  return props.mapData.data && props.mapData.data.data && props.mapData.data.seriesList;
});
//格式化经纬度
const formattedNumber = computed(() => {
  const lat = props.mapData.info.latitude;
  const lng = props.mapData.info.longitude;
  return `${lng},${lat}`;
});
const alarmValue = computed(() => {
  if (props.mapData.data.data.yaxisList && props.mapData.data.data.yaxisList[0]?.unit === 'Mpa') {
    return props.mapData.info.value / 1000000;
  } else {
    return props.mapData.info.value;
  }
});
const isShowAlarmPressure = computed(() => {
  return props.mapData.info.alarmType === '02';
});
const isShowLonLat = computed(() => {
  return props.mapData.info.latitude && props.mapData.info.longitude;
});
const isShowIntelligentAnalyse = computed(() => {
  return props.mapData.info.alarmType === '01' && isShowBtn.value;
});
//点击智能分析事件
const modelingHandle = (mapData) => {
  useIntelligentAnalyticsStore().riskData.value = mapData;
  useAtlasStore().atlasChange('clear');
  router.push('/IntelligentAnalytics');
};
//获取参数设置近某天的运行数据
const OperationalDataName = ref(`近${props.someDay}天运行数据`);
//切换运行数据
const ToggleOperatingData = () => {
  const params = {
    type: undefined,
    id: undefined
  };
  if (OperationalDataName.value === `近${props.someDay}天运行数据`) {
    OperationalDataName.value = `查看报警数据`;
    params.type = 'MONITOR';
    params.id = props.mapData.overhaul.id;
  } else {
    OperationalDataName.value = `近${props.someDay}天运行数据`;
    params.type = 'GAOJINGTYPE';
    params.id = props.mapData.info.id;
  }
  getPointInfo(params).then((res) => {
    emit('update:mapData', {
      ...props.mapData,
      data: res.data.data
    });
  });
};
const EarlyWarningUrl = ref<string>('');
const EarlyWarningTicketsRef = ref();
//查询预警工单
const goAlarmWork = () => {
  EarlyWarningUrl.value = props.mapData.info.workOrderInfoUrl;
  EarlyWarningTicketsRef.value.open();
};
onMounted(async () => {
  try {
    const { data } = await getModelData({ id: props.mapData.info.id });
    useIntelligentAnalyticsStore().modelData.value = data;
    if (data) {
      isShowBtn.value = true;
    }
  } catch (error) {
    console.log(error, '获取模型数据失败');
  }
});
</script>

<template>
  <div class="card-box">
    <el-row :gutter="10">
      <el-col :span="24">
        <el-descriptions :column="2" class="largeScreen-descriptions">
          <el-descriptions-item label="报警类型">{{ mapData.info.alarmTypeName }}</el-descriptions-item>
          <el-descriptions-item label="报警位置">{{ mapData.info.alarmAddr }}</el-descriptions-item>
          <el-descriptions-item label="报警次数">{{ mapData.info.alarmTotal }}</el-descriptions-item>
          <el-descriptions-item label="上报时间">{{ mapData.info.reportTime }}</el-descriptions-item>
          <el-descriptions-item label="报警开始时间">{{ mapData.info.alarmStartTime }}</el-descriptions-item>
          <el-descriptions-item label="最新报警时间">{{ mapData.info.alarmTime }}</el-descriptions-item>
          <el-descriptions-item label="报警持续时间">{{ mapData.info.duration }}</el-descriptions-item>
          <el-descriptions-item label="处置状态">
            <span>{{ mapData.info.statusName }}</span>
            <el-button type="primary" size="small" @click="goAlarmWork" v-if="mapData.info.workOrderInfoUrl"> 查看报警工单 </el-button>
          </el-descriptions-item>
          <el-descriptions-item label="处置时间">{{ mapData.info.processTime }}</el-descriptions-item>
          <el-descriptions-item label="报警压力" v-if="isShowAlarmPressure">{{ alarmValue }}</el-descriptions-item>
          <el-descriptions-item label="经纬度" v-if="isShowLonLat">{{ formattedNumber }}</el-descriptions-item>
          <el-descriptions-item label="阈值最大值" v-if="mapData.info.thresholdMax">
            {{ mapData.info.thresholdMax + (mapData.info.unit || '') }}
          </el-descriptions-item>
          <el-descriptions-item label="阈值最小值" v-if="mapData.info.thresholdMin">
            {{ mapData.info.thresholdMin + (mapData.info.unit || '') }}
          </el-descriptions-item>
          <el-descriptions-item label="智能分析" v-if="mapData.info.alarmType == '01'">
            <el-button v-if="isShowIntelligentAnalyse" type="primary" size="default" @click="modelingHandle(mapData)">智能分析结果</el-button>
            <span v-else>模型暂无分析结果</span>
          </el-descriptions-item>
          <el-descriptions-item label="报警级别" v-if="mapData.info.alarmLevel">{{ mapData.info.alarmLevel }} </el-descriptions-item>
        </el-descriptions>
      </el-col>
    </el-row>

    <el-row :gutter="10">
      <el-col :span="24">
        <div class="title">
          <el-row :gutter="10">
            <el-col :span="16" class="time-period">
              <span>{{ getAlarmTime }}{{ alarmTimePeriod }}</span>
            </el-col>
            <el-col :span="5" class="escalate-records">
              <span @click="ToggleOperatingData" v-if="mapData.data && mapData.overhaul">{{ OperationalDataName }}</span>
            </el-col>
            <el-col :span="3" class="escalate-records">
              <span @click="ReportHandle">报警记录</span>
            </el-col>
          </el-row>
        </div>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <div class="chart-box" v-if="mapData.info.alarmType !== '05'">
          <runData v-if="isShowECharts" :echartsData="mapData.data" :deviceInfo="mapData.info" />
          <el-empty v-else :image="empty" description="暂无数据" class="largeScreen-empty" />
        </div>
        <div class="chart-box" v-else>
          <haikang-h5-video class="video-box" id="alarmVideo" :video-type="0" :value="adjustWebSocketUrl(mapData.data as any)" />
        </div>
      </el-col>
    </el-row>
    <alarmRecords :overhaul="mapData.overhaul" :info="mapData.info" ref="alarmRecordsRef" />
    <EarlyWarningTickets ref="EarlyWarningTicketsRef" :alarm-work-order-url="EarlyWarningUrl" />
  </div>
</template>

<style scoped lang="scss">
.card-box {
  min-width: 700px;
  min-height: 200px;

  .title {
    background: url('@/assets/largescreen/title-pic.png') no-repeat center center;
    background-size: cover;
    width: 100%;
    height: 61px;
    line-height: 61px;
    padding-left: 20px;

    .time-period {
      color: white;
      font-family: PangMenZhengDao;
      font-size: 18px;
    }

    .escalate-records {
      color: #0be4f1;
      font-family: PangMenZhengDao;
      font-size: 18px;
      text-align: right;
      cursor: pointer;

      &:hover {
        text-decoration: underline;
      }
    }
  }

  .chart-box {
    width: 700px;
    height: 280px;

    .video-box {
      width: 100%;
      height: 100%;
    }
  }

  .capacity-box {
    width: 100%;
    height: 50px;
    border: 2px dashed #ccc;
    border-radius: 4px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    .capacity-value {
      font-weight: 700;
      font-size: 16px;
      color: #00ffff;
    }

    .capacity-title {
      font-size: 12px;
      color: #fff;
      opacity: 0.8;
    }
  }

  .empty-box {
    width: 100%;
    height: 50px;
    color: #0be4f1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }
}
</style>
